﻿using A.M.ETO;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using Newtonsoft.Json;
using O.M.Production.Application.IService;
using O.M.Production.Application.Service;
using O.M.Production.Domain.DTO;
using O.M.Production.Domain.Entity;
using O.M.Production.ErrorCodeEnum;
using O.M.Production.Infrastuecure.IRepository;
using Public_Class_Library;
using Q.M.Sell.Doamin.Entity;
using SqlSugar;
using SqlSugar.Extensions;
using System.Linq.Expressions;
using W.M.Materiel.Infrastruecure.DTO;
using static O.M.Production.Domain.EnumModel;

namespace O.M.Production.Read.API.Controllers
{
    /// <summary>
    /// Api控制器-内部生产
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Read_Internal_ProductController : ControllerBase
    {
        private readonly ICapPublisher capPublisher;
        private readonly I_internal_product_Service _internal_product_service;
        public readonly ILogger<Read_Internal_ProductController> _logger;
        private readonly Cross_service_Helper _cross_service_helper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="internal_product_service"></param>
        /// <param name="cross_service_helper"></param>
        public Read_Internal_ProductController(I_internal_product_Service internal_product_service, Cross_service_Helper cross_service_helper, ILogger<Read_Internal_ProductController> logger, ICapPublisher capPublisher)
        {
            _internal_product_service = internal_product_service;
            _cross_service_helper = cross_service_helper;
            _logger = logger;
            this.capPublisher = capPublisher;
        }
        /// <summary>
        /// 日志
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult textLog()
        {
            internal_product internal_Product = null;
            return Ok(internal_Product.produce_theme);
        }
        /// <summary>
        /// 上传文件到七牛云
        /// </summary>
        /// <param name="file">文件</param>
        /// <returns>是否成功</returns>
        [HttpPost]
        public IActionResult AddFile(IFormFile file)
        {
            var res = FileHelper.UploadFile(file);
            return Ok(file.FileName);
        }
        /// <summary>
        /// 跨服务访问接口-必须是HttpGet协议，否则报405
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(string? Url)
        {
            var res = await _cross_service_helper.Get(Url);
            var w = JsonConvert.DeserializeObject(res);
            return Ok(w);
        }
        /// <summary>
        /// 跨服务-获取生产BOM
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<IActionResult> Get_Produce_Bom(string? Url, int pageSize, int pageIndex)
        {
            try
            {
                var list = await _internal_product_service.Get_Produce_Bom(Url, pageSize, pageIndex);
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 分页查询-内部生产
        /// </summary>
        /// <param name="IPName">模糊查询编号或者名称</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="produce_type">生产类型</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<ApiResult> Get_internal_product_Page(string? IPName, Produce_Type? produce_type, int pageSize, int pageIndex)
        {
            try
            {
                var list = await _internal_product_service.Get_internal_product_Page(IPName, produce_type, pageSize, pageIndex);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 返回内部生产实体
        /// </summary>
        /// <param name="internal_product_id">内部生产编号</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<IActionResult> Get_ReturmModel_internal_product(string? internal_product_id)
        {
            try
            {
                var res = await _internal_product_service.Get_ReturmModel_internal_product(internal_product_id);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 加载生产类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get_Produce_Type()
        {
            try
            {
                List<object> list = new List<object>();
                foreach (var item in Enum.GetValues<Produce_Type>())
                {
                    list.Add(new
                    {
                        id = item,
                        name = item.ToString(),
                    });
                }
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 加载所在部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get_Department()
        {
            try
            {
                List<object> list = new List<object>();
                foreach (var item in Enum.GetValues<Department>())
                {
                    list.Add(new
                    {
                        id = item,
                        name = item.ToString(),
                    });
                }
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 加载生产部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get_Produce_Department()
        {
            try
            {
                List<object> list = new List<object>();
                foreach (var item in Enum.GetValues<Produce_Department>())
                {
                    list.Add(new
                    {
                        id = item,
                        name = item.ToString(),
                    });
                }
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 加载负责人
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get_Person_In_Charge()
        {
            try
            {
                List<object> list = new List<object>();
                foreach (var item in Enum.GetValues<Person_In_Charge>())
                {
                    list.Add(new
                    {
                        id = item,
                        name = item.ToString(),
                    });
                }
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 加载生产人
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get_Produce_Personnel()
        {
            try
            {
                List<object> list = new List<object>();
                foreach (var item in Enum.GetValues<Produce_Personnel>())
                {
                    list.Add(new
                    {
                        id = item,
                        name = item.ToString(),
                    });
                }
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加-内部生产
        /// </summary>
        /// <param name="product_DTO">内部生产DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> Post_Add_internal_product(internal_product_DTO product_DTO)
        {
            try
            {
                var res = await _internal_product_service.Post_Add_internal_product(product_DTO);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        [CapSubscribe("GetScName")]
        public async Task<IActionResult> GetAddXsInfor(string? id)
        {
            var res = await _internal_product_service.GetAddXsInfor(id);
            return Ok(res);
        }

        /// <summary>
        /// 审批的消息队列
        /// </summary>
        /// <param name="addStaffInforDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddInforMQ(AprInforGerDTO addStaffInforDTO)
        {
            //申请成功需要传一个消息队列;
            capPublisher.Publish("addStaffInforDTOGer", addStaffInforDTO);
            return Ok("");
        }
        /// <summary>
        /// 先获取所有的审批
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProccInfir()
        {
            var res = "http://10.31.59.5:5123/api/Approval_form/GetappList";
            var mes = await _cross_service_helper.Get(res);
            var ges = JsonConvert.DeserializeObject<List<AddStaffInforDTO>>(mes);
            var list = ges.Where(c => c.approval_name == "销售系统").FirstOrDefault();
            return Ok(list.apprpvalTwoDTO);
        }
        /// <summary>
        /// 批量删除-内部生产
        /// </summary>
        /// <param name="product_DTO">内部生产DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> Post_Batch_Del_internal_produc(List<internal_product_DTO> product_DTO)
        {
            try
            {
                var res = await _internal_product_service.Post_Batch_Del_internal_produc(product_DTO);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 修改-内部生产
        /// </summary>
        /// <param name="product_DTO">内部生产DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public IActionResult Post_Upd_internal_produc(internal_product_DTO product_DTO)
        {
            try
            {
                var res = _internal_product_service.Post_Upd_internal_produc(product_DTO);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 导出列表到Excel
        /// </summary>
        /// <returns>Excel文件</returns>
        [HttpGet]
        public async Task<IActionResult> Post_Derive_Excel()
        {
            try
            {
                var list = await _internal_product_service.GetShowInforAsync();
                var ms = new MemoryStream();
                ms.SaveAs(list);
                ms.Seek(0, SeekOrigin.Begin);
                return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    FileDownloadName = "内部生产.xlsx"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 从七九云上下载文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Load_Attachment(string? fileName)
        {
            try
            {
                var res = await FileHelper.DownloadFileAsync(fileName);
                return Ok(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
